package ro.mse.search.main;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

import ro.mse.search.analyzer.SnowballAnalyzer;

/**
 * 
 * @author Razvan Darju 
 * Test class for indexing
 */
public class Indexer {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		if (args.length != 2) {
			throw new Exception("Usage: java " + Indexer.class.getName()
					+ " <index dir> <data dir>");
		}
		String indexDir = args[0]; // 1
		String dataDir = args[1]; // 2
		long start = System.currentTimeMillis();
		Indexer indexer = new Indexer(indexDir);
		int numIndexed = indexer.index(dataDir);
		indexer.close();
		long end = System.currentTimeMillis();
		System.out.println("Indexing " + numIndexed + " files took "
				+ (end - start) + " milliseconds");
	}

	private IndexWriter writer;

	public Indexer(String indexDir) throws IOException {
		writer = new IndexWriter(
				indexDir, // 3
				new SnowballAnalyzer("RoAnalyzer"), true,
				IndexWriter.MaxFieldLength.UNLIMITED);
	}

	public void close() throws IOException {
		writer.close(); // 4
	}

	public int index(String dataDir) throws Exception {
		File[] files = new File(dataDir).listFiles();

		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			if (!f.isDirectory() && !f.isHidden() && f.exists() && f.canRead()
					&& acceptFile(f)) {
				indexFile(f);
			}
		}
		return writer.numDocs(); // 5
	}

	protected boolean acceptFile(File f) { // 6
		return f.getName().toLowerCase().endsWith(".txt");
	}

	protected Document getDocument(File f) throws Exception {
		Document doc = new Document();
		doc.add(new Field("contents", new FileReader(f))); // 7
		doc.add(new Field("filename", f.getCanonicalPath(), // 8
				Field.Store.YES, Field.Index.ANALYZED));
		return doc;
	}

	private void indexFile(File f) throws Exception {
		System.out.println("Indexing " + f.getCanonicalPath());
		Document doc = getDocument(f);
		if (doc != null) {
			writer.addDocument(doc); // 9
		}
	}

}
